package org.hujunfeng.http;

/**
 * @author 胡俊丰
 * @version 1.0.0
 * <p>
 * date: 2024/10/5
 **/
import java.io.ByteArrayOutputStream;

/**
 * @Author 胡俊丰
 * @Date 2024/3/17
 **/
public class URLTool {

    /**
     * 编码URL格式
     * @param url
     * @param isNormal
     * @return
     */
    public static String encode(String url, boolean isNormal){
        StringBuffer buffer = new StringBuffer();
        char letters[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        if (url != null && !url.equals("")){
            char temp;
            for (int i = 0; i < url.length(); i++) {
                temp = url.charAt(i);

                // 如果必须要转换的字符
                if (isRequire(temp) || (isSpacial(temp) && !isNormal)){
                    byte[] data = String.valueOf(temp).getBytes();
                    int d = 0;
                    for (int h = 0; h < data.length; h++) {
                        d = data[h] & 0xFF;
                        buffer.append("%");
                        buffer.append(letters[(d & 0xF0) >> 4]);
                        buffer.append(letters[(d & 0x0F)]);
                    }
                } else {
                    // 原样保存
                    buffer.append(temp);
                }
            }
        }
        return buffer.toString();
    }

    /**
     * 使用普通模式编码URL
     * @param url
     * @return
     */
    public static String encode(String url){
        return encode(url, true);
    }

    /**
     * 解码方法
     * @param url
     * @return
     */
    public static String decode(String url) {
        StringBuffer buffer = null;

        if (url != null && !url.equals("")){
            buffer =  new StringBuffer();

            char temp = '\0';

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            for (int i = 0; i < url.length(); i++) {
                temp = url.charAt(i);
                if (temp == '%') {
                    i++;
                    int b1 = getInt(url.charAt(i));

                    i++;
                    int b2 = getInt(url.charAt(i));

                    int data = (b1 << 4) | (b2);
                    baos.write(data);
                } else {
                    if (baos.size() > 0){
                        buffer.append(new String(baos.toByteArray()));
                        baos.reset();
                    }
                    buffer.append(temp);
                }
            }

            if (baos.size() > 0){
                buffer.append(new String(baos.toByteArray()));
                baos.reset();
            }
        }
        return buffer.toString();
    }

    /**
     * 传入一个十六进制数，返回对应的值
     * @param k
     * @return
     */
    private static int getInt(char k){
        int result = 0;
        if (k >= '0' && k <= '9'){
            result = k - '0';
        } else if (k >= 'a' && k <= 'f'){
            result = 10 + (k - 'a');
        } else if (k >= 'A' && k <= 'F'){
            result = 10 + (k - 'A');
        }
        return result;
    }

    /**
     * 判断是否为必须要转换的字符
     * @param k
     * @return
     */
    private static boolean isRequire(char k){
        return String.valueOf(k).matches("[^0-9a-zA-Z/\\.=&#\\?:]");
    }

    /**
     * 判断是否为保留的特殊字符
     * @param k
     * @return
     */
    private static boolean isSpacial(char k){
        return String.valueOf(k).matches("[/\\.=&#\\?:@]");
    }
}
